"""
项目相关的Pydantic模式
"""
from datetime import datetime
from typing import Dict, List, Optional, Any
from pydantic import BaseModel, Field, ConfigDict

from app.models.project import ScenarioType, ProjectStatus


class ProjectBase(BaseModel):
    """项目基础模式"""
    name: str = Field(..., min_length=1, max_length=100, description="项目名称")
    description: Optional[str] = Field(None, max_length=1000, description="项目描述")
    scenario_type: ScenarioType = Field(..., description="场景类型")
    config: Dict[str, Any] = Field(default_factory=dict, description="项目配置")
    tags: List[str] = Field(default_factory=list, description="项目标签")
    is_public: bool = Field(default=False, description="是否公开")


class ProjectCreate(ProjectBase):
    """项目创建模式"""
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "name": "智慧照明项目",
                "description": "基于IoT传感器的智能路灯控制系统",
                "scenario_type": "smart_lighting",
                "config": {
                    "data_collection_interval": 300,
                    "model_type": "transformer",
                    "batch_size": 32
                },
                "tags": ["IoT", "智慧城市", "照明"],
                "is_public": True
            }
        }
    )


class ProjectUpdate(BaseModel):
    """项目更新模式"""
    name: Optional[str] = Field(None, min_length=1, max_length=100, description="项目名称")
    description: Optional[str] = Field(None, max_length=1000, description="项目描述")
    config: Optional[Dict[str, Any]] = Field(None, description="项目配置")
    tags: Optional[List[str]] = Field(None, description="项目标签")
    is_public: Optional[bool] = Field(None, description="是否公开")
    status: Optional[ProjectStatus] = Field(None, description="项目状态")


class ProjectInDB(ProjectBase):
    """数据库中的项目模式"""
    id: int
    status: ProjectStatus
    owner_id: int
    training_jobs_count: int
    models_count: int
    data_sources_count: int
    created_at: datetime
    updated_at: datetime
    
    model_config = ConfigDict(from_attributes=True)


class Project(ProjectBase):
    """项目响应模式"""
    id: int
    status: ProjectStatus
    owner_id: int
    training_jobs_count: int
    models_count: int
    data_sources_count: int
    created_at: datetime
    updated_at: datetime
    
    model_config = ConfigDict(from_attributes=True)


class ProjectSummary(BaseModel):
    """项目摘要模式"""
    id: int
    name: str
    scenario_type: ScenarioType
    status: ProjectStatus
    owner_id: int
    training_jobs_count: int
    models_count: int
    data_sources_count: int
    created_at: datetime
    updated_at: datetime
    
    model_config = ConfigDict(from_attributes=True)


class ProjectList(BaseModel):
    """项目列表模式"""
    projects: List[ProjectSummary]
    total: int
    page: int
    page_size: int


class ProjectStats(BaseModel):
    """项目统计模式"""
    total_projects: int
    active_projects: int
    archived_projects: int
    training_jobs_total: int
    models_total: int
    data_sources_total: int
    projects_by_scenario: Dict[str, int]
    recent_activities: List[Dict[str, Any]]


class ProjectExport(BaseModel):
    """项目导出模式"""
    project: Project
    data_sources: List[Dict[str, Any]]
    training_jobs: List[Dict[str, Any]]
    models: List[Dict[str, Any]]
    export_timestamp: datetime


class ProjectTemplate(BaseModel):
    """项目模板模式"""
    id: str
    name: str
    description: str
    scenario_type: ScenarioType
    config_template: Dict[str, Any]
    required_data_sources: List[str]
    recommended_model_types: List[str]
    example_datasets: List[str]